#include <iostream>
#include<string.h>
using namespace std;

class String
{
public:
	String();
	String(const char *pstr);
	String(const String &rhs);
	String &operator=(const String &rhs);
	~String();
	void print();
    size_t length() const;
    const char * c_str() const;

private:
	char * _pstr;
};
String::String()
    :_pstr(new char[1]())    
    {
        * _pstr='\0';
    }
//常量初始化
String::String(const char *pstr)
    :_pstr(new char[strlen(pstr)+1]())
    {
        strcpy(_pstr,pstr);
    }
//拷贝构造
String::String(const String &rhs)
    :_pstr(new char[strlen(rhs._pstr)+1]())
    {
        strcpy(_pstr,rhs._pstr);
    }

//赋值运算符
String & String::operator=(const String &rhs)
{
    if(_pstr){
        delete [] _pstr;
    }
    _pstr = new char[strlen(rhs._pstr)+1]();
    strcpy(_pstr,rhs._pstr);
    return *this;
}

String::~String(){
    if(_pstr){
        delete [] _pstr;
        _pstr = nullptr;
    }
    cout<<"~String"<<endl;
}

void String:: print(){
    cout<<_pstr<<endl;
}

size_t String::length() const{
   size_t len;
   len = strlen(_pstr);
    return len;
}
const char * String::c_str() const{
    return _pstr;
}

int main()
{
	String str1;
	str1.print();
	

	String str2 = "Hello,world";
	String str3("wangdao");
	
	str2.print();		
	str3.print();	
	
	String str4 = str3;
	str4.print();
	
	str4 = str2;
	str4.print();
	
	return 0;
}
